package com.ly.hotel.view.fragments.intro;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.model.Serve;
import com.ly.hotel.util.ImageUtil;
import com.ly.hotel.util.LanguageUtil;
import com.ly.hotel.view.common.BaseActivity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class IntroActivity extends BaseActivity {
	private ViewPager mViewPager;
	private LinearLayout mLinearLayout;
	private ImageView leftImage;
	private ImageView rightImage;

	private List<Serve> gridViewList;
	private List<List<Serve>> listList = new ArrayList<>();

	private LayoutInflater mInflater;
	private int rod;
	private IntroDetailsDialog detailsDialog;

	public static void actionStart(Context context, String title, List<Serve> data, int position){
		Intent intent = new Intent(context,IntroActivity.class);
		intent.putExtra("title", title);
		intent.putExtra("position", position);
		Bundle bundle  =new Bundle();
		bundle.putSerializable("data",(Serializable) data);//序列化  
		intent.putExtras(bundle);//发送数据 
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intro);
		Intent intent = getIntent();
		String name = intent.getStringExtra("title");
		gridViewList = (List<Serve>) intent.getSerializableExtra("data");
		rod = getResources().getDimensionPixelSize(R.dimen.px9);

		findView();
		setValue();
		setListener();
	}

	private void findView() {
		mInflater = getLayoutInflater();
		mViewPager = findViewById(R.id.viewpager);
		leftImage = findViewById(R.id.intro_left_more);
		rightImage = findViewById(R.id.intro_right_more);
		detailsDialog = new IntroDetailsDialog(this,R.style.introDialog);
	}

	private void setValue() {
		if(gridViewList == null){
			return;
		}
		listList.clear();
		int t = gridViewList.size();
		for (int i = 0; i < t; i = i + 6) {
			int fromIndex = i;
			int toIndex = i + 6;
			if (toIndex >= t) {
				toIndex = t;
			}
			List<Serve> nv = gridViewList.subList(fromIndex, toIndex);
			listList.add(nv);
		}

		MyPagerAdapter mAdapter = new MyPagerAdapter();
		mViewPager.setAdapter(mAdapter);
		mViewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.px120));
		mViewPager.setOffscreenPageLimit(3);
		mViewPager.post(new Runnable() {
			@Override
			public void run() {
				if (listList.size() > 1){
					rightImage.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	private void setListener() {
		mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				int c = listList.size();
				if(c > 1){
					if(position == 0){
						leftImage.setVisibility(View.INVISIBLE);
						rightImage.setVisibility(View.VISIBLE);
					} else if(position + 1 == c){
						rightImage.setVisibility(View.INVISIBLE);
						leftImage.setVisibility(View.VISIBLE);
					} else {
						leftImage.setVisibility(View.VISIBLE);
						rightImage.setVisibility(View.VISIBLE);
					}
				}
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});

		leftImage.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				int nPos = mViewPager.getCurrentItem();
				if(nPos > 0){
					mViewPager.setCurrentItem(nPos-1);
				}
			}
		});

		rightImage.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				int nPos = mViewPager.getCurrentItem();
				if((nPos + 1) < listList.size()){
					mViewPager.setCurrentItem(nPos+1);
				}
			}
		});
	}

	class MyPagerAdapter extends PagerAdapter {

		public MyPagerAdapter(){}

		@Override
		public int getCount() {
			return listList.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public Object instantiateItem(ViewGroup container, final int position) {


			View view = mInflater.inflate(R.layout.intro_pager_item,container,false);
			container.addView(view);
			GridView gdl = view.findViewById(R.id.intro_grid);
			List<Serve> serveList = listList.get(position);
			IntroItemAdapter itemAdapter = new IntroItemAdapter(serveList);
			gdl.setAdapter(itemAdapter);
			gdl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
					if(listList.get(position).get(pos).getSiList() != null && listList.get(position).get(pos).getSiList().size() >0){
						IntroActivity.actionStart(IntroActivity.this, "", listList.get(position).get(pos).getSiList(),0);
					}else {
						detailsDialog.show();
						detailsDialog.setServe(listList.get(position).get(pos));
					}
				}
			});
			return view;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}


	class IntroItemAdapter extends BaseAdapter {
		List<Serve> itemList;

		public IntroItemAdapter(List<Serve> list){
			this.itemList = list;
		}

		@Override
		public int getCount() {
			return itemList.size();
		}

		@Override
		public Object getItem(int position) {
			return itemList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int postion, View contentView, ViewGroup arg2) {
			ViewHolder holder;
			if (contentView == null) {
				holder = new ViewHolder();
				contentView = mInflater.inflate(R.layout.intro_item,arg2,false);
				holder.mImg = contentView.findViewById(R.id.introImage);
				holder.mTxt= contentView.findViewById(R.id.introName);
				contentView.setTag(holder);
			} else {
				holder = (ViewHolder) contentView.getTag();
			}
			Serve item = itemList.get(postion);
			String imgPath = MyApplication.getHostResUrl() + "upload/"
					+ item.getImg();
			ImageUtil.displayImage_Round(IntroActivity.this, imgPath, holder.mImg,rod);
			String name = LanguageUtil.getTextFromLocalLanguage(item.getName());
			holder.mTxt.setText(name);
			return contentView;
		}

		class ViewHolder {
			ImageView mImg;
			TextView mTxt;
		}
	}
}
